package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Address;
import com.mju.service.IAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/addresses")
@Api(tags = "地址信息服务")
public class AddressController extends BaseController {
    @Autowired
    private IAddressService addressService;

    // 增加收获地址
    @ApiOperation("增加收获地址")
    @PostMapping("/add_new_address")
    public JsonResult<Void> addNewAddress(@RequestBody Address address, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");
        addressService.addNewAddress(Integer.parseInt(uid), uname, address);
        return new JsonResult<>(OK);
    }

    // 展示收货地址数据
    @ApiOperation("展示收货地址")
    @GetMapping
    public JsonResult<List<Address>> getByUid(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        List<Address> data = addressService.getByUid(Integer.parseInt(uid));
        return new JsonResult<>(OK, data);
    }

    // 设置当前用户所选的地址为默认地址
    @ApiOperation("设置当前用户所选的地址为默认地址")
    @GetMapping("{aid}/set_default")
    public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");

        addressService.setDefault(aid, Integer.parseInt(uid), uname);
        return new JsonResult<>(OK);
    }

    // 删除当前用户所选择的收获地址
    @ApiOperation("删除当前用户所选择的收获地址")
    @GetMapping("{aid}/delete")
    public JsonResult<Void> delete(@PathVariable("aid") Integer aid, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");

        addressService.delete(aid, Integer.parseInt(uid), uname);
        return new JsonResult<>(OK);
    }

    @ApiOperation("根据用户id查询收获地址")
    @GetMapping("/queryAllAddress/{uid}")
    public JsonResult<List<Address>> queryAllAddresses(@PathVariable("uid")Integer uid){
        List<Address> data = addressService.queryAllAddressesByUid(uid);
        return new JsonResult<>(OK,data);
    }

    @ApiOperation("无条件查询收货地址")
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<Address>> queryAllAddressInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        IPage<Address> data = addressService.queryAllAddressInfo(currentPage, pageSize);
        return new JsonResult<>(OK,data);
    }

    @ApiOperation("查询收货地址")
    @GetMapping("/getAddress/{uid}/{aid}")
    public JsonResult<Address> getAddress(@PathVariable("uid") Integer uid, @PathVariable("aid") Integer aid) {
        Address address = addressService.getByAid(aid, uid);
        return new JsonResult<>(OK, address);
    }

    @ApiOperation("根据地址id查询收货地址")
    @GetMapping("/getAddress/{aid}")
    public JsonResult<Address> getAddress(@PathVariable("aid") Integer aid) {
        Address address = addressService.queryAddressByAid(aid);
        return new JsonResult<>(OK, address);
    }

    @ApiOperation("根据地址查询收货地址")
    @GetMapping("/queryAddress/{currentPage}/{pageSize}")
    public JsonResult<IPage<Address>> getAddressByName(@PathVariable("currentPage") Integer currentPage,
                                                 @PathVariable("pageSize") Integer pageSize,
                                                 @RequestParam String name) {
        IPage<Address> data = addressService.queryAddressByName(currentPage, pageSize, name);
        return new JsonResult<>(OK, data);
    }
    @ApiOperation("根据用户id查询收货地址")
    @GetMapping("/queryAddressByUid/{currentPage}/{pageSize}")
    public JsonResult<IPage<Address>> getAddressByUid(@PathVariable("currentPage") Integer currentPage,
                                                 @PathVariable("pageSize") Integer pageSize,
                                                 @RequestParam Integer uid) {
        IPage<Address> data = addressService.queryAddressByUid(currentPage, pageSize, uid);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("根据用户电话号码查询收货地址")
    @GetMapping("/queryAddressByPhone/{currentPage}/{pageSize}")
    public JsonResult<IPage<Address>> getAddressByPhone(@PathVariable("currentPage") Integer currentPage,
                                                 @PathVariable("pageSize") Integer pageSize,
                                                 @RequestParam String phone) {
        IPage<Address> data = addressService.queryAddressByPhone(currentPage, pageSize, phone);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("修改地址")
    @PostMapping("/updateAddress/{aid}")
    public JsonResult<Integer> updateAddress(@PathVariable Integer aid, @RequestBody Address address, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        //String uname = map.get("uname");
        Integer data = addressService.updateAddress(aid, Integer.valueOf(uid), address);
        return new JsonResult<>(OK, data);
    }
}
